<template>
	<view>
		<view class="header">
			重新设置你的登录密码
		</view>
		<view class="form re">
			<view class="code">
				<!-- <input placeholder="6-8位数字或者字母" v-model="passwd" placeholder-style="color: rgba(255,255,255,0.8);"/> -->
				<input placeholder="6-8位数字或者字母" v-model="passwd" :password="confirm" placeholder-style="color: rgba(172,173,174,0.8);" />
				<view class="pwd" @tap="changeStatus(0)">
					<image src="/static/img/public/hidePassword.png" v-show="confirm" mode="widthFix"></image>
					<image src="/static/img/public/showPassword.png" v-show="!confirm" mode="widthFix"></image>
				</view>
			</view>
			<view class="password">
				<input placeholder="再次输入你的密码" v-model="comfirm_passwd" :password="confirmAgain" placeholder-style="color: rgba(172,173,174,0.8);" />
				<view class="pwd" @tap="changeStatus(1)">
					<image src="/static/img/public/hidePassword.png" v-show="confirmAgain" mode="widthFix"></image>
					<image src="/static/img/public/showPassword.png" v-show="!confirmAgain" mode="widthFix"></image>
				</view>
			</view>
			<view class="btn" @tap="doReset">确定</view>
		</view>

	</view>
</template>

<script>
	import {
		mapMutations,
		mapState
	} from 'vuex';
	import Api from '@/common/api';
	import store from '@/store/index';
	import md5 from "@/common/SDK/md5.min.js";
	import JSEncrypt from "@/common/SDK/jsencrypt";
	export default {
		data() {
			return {
				phoneNumber: "",
				passwd: "",
				logining: false,
				comfirm_passwd: "",
				publicKey: "",
				privateKey: "",
				confirm: true, //第一个输入框的密码是否隐藏
				confirmAgain: true, //第二个输入框的密码是否隐藏
			}
		},
		onLoad: function(option) {
			this.phoneNumber = option.mobile;
			console.log('设置密码');
			console.log(this.phoneNumber);
		},
		methods: {
			...mapMutations(['login']),
			async doReset() {
				uni.hideKeyboard()
				var _this = this;
				if (!_this.passwd || !_this.comfirm_passwd) {
					uni.showToast({
						title: '请输入设置密码!',
						icon: "none"
					});
					return false;
				}
				if (_this.passwd != _this.comfirm_passwd) {
					uni.showToast({
						title: '密码不一致，请重新输入密码!',
						icon: "none"
					});
					return false;
				}
				uni.showLoading({
					title: '提交中...'
				})
				var secret_key = uni.getStorageSync('secret_key'); //获取服务器公钥
				_this.privateKey = uni.getStorageSync('privateKey');
				_this.publicKey = uni.getStorageSync('publicKey');
				var info = JSON.stringify({
					"mobile": _this.phoneNumber,
					"password": _this.passwd
				});
				var encrypt = new JSEncrypt({
					default_key_size: 1024
				});
				// encrypt.getKey();
				encrypt.setPublicKey(secret_key);
				// var encrypt_txt = rsa.encrypt(info, key, pkcs=8)
				var encrypt_txt = encrypt.encrypt(info); //生成加密字符串		
				var params = {
					"secret_key": secret_key,
					"content": encrypt_txt,
					"client_key": _this.publicKey
				}
				let data = await Api.apiCall('post', Api.index.updatepwd, params);
				if (data) {
					// 模拟用户密码登录 statrt 
					_this.logining = true;
					var parm_info = JSON.stringify({
						"type": "dfdf",
						"mobile": _this.phoneNumber,
						"secret": _this.passwd
					});
					var encrypt_txt2 = encrypt.encrypt(parm_info); //生成加密字符串
					params = {
						"secret_key": secret_key,
						"content": encrypt_txt2,
						"client_key": _this.publicKey
					}
					let result = await Api.apiCall('post', Api.index.login, params);
					if (result) {
						var decryptor = new JSEncrypt({
							default_key_size: 1024
						});
						decryptor.setPrivateKey(_this.privateKey);
						var decrypt_txt = decryptor.decrypt(result.data.content); //解密
						decrypt_txt = JSON.parse(decrypt_txt); //json字符串转Json对像
						if (decrypt_txt.token) {
							var userInfo = {
								sid: result.sid,
								mobile: _this.phoneNumber,
								token: decrypt_txt.token
							}
							_this.login(userInfo); //保存登录缓存
							uni.showToast({
								title: '重置密码成功',
								icon: "success"
							});
							setTimeout(function() {
								uni.switchTab({
									url: '/pages/index/index'
								})
							}, 1000)
						} else {
							_this.logining = false;
						}
					}
					// 模拟用户密码登录 end
				}
			},
			changeStatus(status) {
				switch (status) {
					case 0:
						this.confirm = !this.confirm
						break;
					case 1:
						this.confirmAgain = !this.confirmAgain
						break;
				}
			},
		}
	}
</script>

<style lang="scss">
	@import "../../static/css/passwd.scss";
</style>
